package com.zgm.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 张公铭 on 2017/6/19 0019.
 * 获取设备相信信息
 */

public class DeviceUtils
{
    public static Map<String, String> collectDeviceInfo(Context context)
    {
        Map<String, String> infoMap = new HashMap<String, String>();
        try
        {
            PackageManager pm = context.getPackageManager();
            PackageInfo pInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (pInfo != null)
            {
                String versionName = pInfo.versionName == null ? "null" : pInfo.versionName;
                String versionCode = String.valueOf(pInfo.versionCode);
                infoMap.put("versionName", versionName);
                infoMap.put("versionCode", versionCode);
            }
        } catch (Exception e)
        {
            Log.e("DeviceUtils", "an error occured when getpackage ingo", e);
        }

        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields)
        {
            try
            {
                field.setAccessible(true);
                infoMap.put(field.getName(), field.get(null).toString());
            } catch (Exception e)
            {
                Log.e("DeviceUtils", "an error occured when getpackage ingo", e);
            }
        }
        return infoMap;
    }
}
